package com.jlkf.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jlkf.dao.TblCourseMapper;
import com.jlkf.service.PayOrderService;
import com.platform.server.response.response.pojo.ReturnData;

/**
 * Created by Administrator on 2017/5/17 0017.
 */
@RequestMapping(value = "/payorder")
@Controller
public class PayOrderController extends BaseController{

    @Autowired
    private PayOrderService payOrderService;
    
    
	
	@Autowired
	private TblCourseMapper tblCourseMapper;
    /**
     * 余额充值
     */
    @RequestMapping(value = "/balanceRecharge",method=RequestMethod.POST)
    @ResponseBody
    public ReturnData balanceRecharge(Long userId,Double payMoney,Integer payWay) throws Exception{
    	return payOrderService.BalanceRecharge(userId, payMoney, getRemoteHost(request),payWay);
    }
    
    /**
     * 充值余额微信回调
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/wxBalance")
    @ResponseBody
    public void wxBalance(HttpServletResponse response) throws Exception{
        payOrderService.wxBalance(request, response);
    }
    
    /**
     * 充值余额支付宝回调
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/aliRechargeNotify")
    @ResponseBody
    public Object aliRechargeNotify(HttpServletResponse response) throws Exception{
       return payOrderService.aliRechargeNotify(request, response);
    }

    /**
     * 开通会员微信回调
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/wxNotify")
    @ResponseBody
    public void wxNotify(HttpServletResponse response) throws Exception{
        payOrderService.wxNotify(request, response);
    }

    /**
     * 开通会员支付宝回调
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/aliNotify")
    @ResponseBody
    public Object aliNotify(HttpServletResponse response) throws Exception{
       return payOrderService.aliNotify(request, response);
    }
    
    public String getRemoteHost(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
	}
    
    /**
     * 购买课程支付宝回调
     */
    @RequestMapping(value = "/payCourseNotify")
    @ResponseBody
    public Object payCourseNotify(HttpServletResponse response) throws Exception{
       return payOrderService.payCourseNotify(request, response);
    }
    
    /**
     * 购买课程微信回调
     */
    @ResponseBody
    @RequestMapping(value = "/wxPayCourse")
    public void wxPayCourse(HttpServletResponse response) throws Exception{
        payOrderService.wxPayCourse(request, response);
    }
    
}
